package org.gushi.datastruct.a04.list;

/**
 * 抽取公共代码部分，对外界不可用  不建议使用常量放入此类中，应放在接口类中对外是公开
 * @param <E>
 */
public abstract class AbstractList<E> implements List<E> {
    protected int size;
    protected void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size);
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(E element) {
        this.add(size,element);
    }

    @Override
    public boolean isEmpty() {
        return size <= 0;
    }

    @Override
    public boolean isContains(E element) {
        return indexOf(element) != ELEMENT_NOT_FOUND;
    }


}
